package model.dao.registerOrder;

import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import db.dbconnection;

import log.MyLogger;
import model.service.ServiceType;
import model.service.ServiceUnitType;

public class ServiceTypeDAOImpl {
	
	String sql = "" ;
	db.dbconnection dbconnection ;
	java.sql.Connection myConnection ;
	Statement st ;
	
	public ServiceType getServicType(int serviceType) {
		
		ServiceType sType = new ServiceType();
		try {
		    dbconnection = new dbconnection();
		    myConnection = dbconnection.getConnection();
		    st = myConnection.createStatement();
			String sql = "SELECT service_type.*,service_unit_type.type_name AS service_unit_type_type_name FROM service_type LEFT JOIN service_unit_type ON (service_type.service_unit_type_fk = service_unit_type.service_unit_type) WHERE service_type = "+ serviceType;
			ResultSet r = st.executeQuery(sql);
			
			while(r.next()){
				sType.setServiceType(r.getInt("service_type"));
				sType.setServicePrice(r.getFloat("service_price"));
				sType.setTypeName(r.getString("type_name"));
				ServiceUnitType serviceUnitType = new ServiceUnitType();
				serviceUnitType.setServiceUnitType(r.getInt("service_unit_type_fk"));
				serviceUnitType.setTypeName(r.getString("service_unit_type_type_name"));
				sType.setServiceUnitType(serviceUnitType);
			}
			this.dbconnection.close();
		} catch (Exception e) {
//			MyLogger.Log("ServiceOrderDAOImpl.getServiceOrder()", e.getMessage());
		}
		return sType;
	}

	public List<ServiceType> getAllTypes() {
		List<ServiceType> types = new ArrayList<ServiceType>();
		try {
		    dbconnection = new dbconnection();
		    myConnection = dbconnection.getConnection();
		    st = myConnection.createStatement();
			String sql = "SELECT service_type.*,service_unit_type.type_name AS service_unit_type_type_name FROM service_type LEFT JOIN service_unit_type ON (service_type.service_unit_type_fk = service_unit_type.service_unit_type)";
			ResultSet r = st.executeQuery(sql);
			
			while(r.next()){
				ServiceType serviceType = new ServiceType();
				serviceType.setServiceType(r.getInt("service_type"));
				serviceType.setServicePrice(r.getFloat("service_price"));
				serviceType.setTypeName(r.getString("type_name"));
				types.add(serviceType);

				ServiceUnitType serviceUnitType = new ServiceUnitType();
				serviceUnitType.setServiceUnitType(r.getInt("service_unit_type_fk"));
				serviceUnitType.setTypeName(r.getString("service_unit_type_type_name"));
				serviceType.setServiceUnitType(serviceUnitType);
			}
		} catch (Exception e) {
//			MyLogger.Log("ServiceOrderDAOImpl.getAllTypes()", e.getMessage());
			return null;
		}
		return types;
	}



}
